using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using Microsoft.TeamFoundation.WorkItemTracking.Common;
using Microsoft.TeamFoundation.WorkItemTracking.Controls;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System.Collections.Specialized;
using System.Drawing.Imaging;
using System.IO;


namespace UKVSTS.WorkItemControls
{
    public partial class ScreenShotWorkItemControl : UserControl, IWorkItemControl
    {

        IServiceProvider m_ServiceProvider;

        WorkItem m_DataSource;

        String m_FieldName;

        bool m_IsReadOnly;

        StringDictionary m_CustomProperties;

        String m_TempDir;

        List<String> m_AttachedFiles = new List<string> ();

        public ScreenShotWorkItemControl()
        {
            InitializeComponent();
        }

        public event EventHandler AfterUpdateDatasource;

        public event EventHandler BeforeUpdateDatasource;

        public void Clear()
        {
        }

        public void FlushToDatasource()
        {
        }

        public void InvalidateDatasource()
        {
        }

        public StringDictionary Properties
        {
            get
            {
                return m_CustomProperties;
            }
            set
            {
                m_CustomProperties = value;
            }
        }

        public bool ReadOnly
        {
            get
            {
                return m_IsReadOnly;
            }
            set
            {
                m_IsReadOnly = value;
            }
        }

        public void SetSite(IServiceProvider serviceProvider)
        {
            m_ServiceProvider = serviceProvider;
        }

        public object WorkItemDatasource
        {
            get
            {
                return m_DataSource;
            }
            set
            {
                m_DataSource = (WorkItem)value;
            }
        }

        public string WorkItemFieldName
        {
            get
            {
                return m_FieldName;
            }
            set
            {
                m_FieldName = value;
            }
        }

        private void cmdAddScreenShot_Click(object sender, EventArgs e)
        {
            try
            {
                if (Clipboard.ContainsImage() == false)
                {
                    MessageBox.Show(this,
                                     "There is no image in the clipboard.\n"
                                     + "Use the Print Screen key to capture a screenshot first.\n"
                                     + "(or Alt + PrtSc to capture just the current window)",
                                     "No Screenshot",
                                     MessageBoxButtons.OK,
                                     MessageBoxIcon.Error);
                }
                else
                {

                    String fileName = null;
                    String comment = null;

                    using (Image screenShot = Clipboard.GetImage())
                    {
                        // Need a dir to write any screenshots to
                        //
                        if (String.IsNullOrEmpty(m_TempDir))
                        {
                            m_TempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
                            Directory.CreateDirectory(m_TempDir);
                        }

                        bool found;
                        int index = 0;
                        String suggestedFileName;

                        do {
                            found = false;
                            suggestedFileName = String.Concat("Screenshot ", index.ToString(), ".png");

                            foreach (Attachment attchment in m_DataSource.Attachments) {

                                if (attchment.Name.Equals(suggestedFileName))
                                {
                                    found = true;
                                    index++;
                                    break;
                                }
                            }
                        } while (found == true);


                        using (Edit imageEditForm = new Edit(screenShot, m_TempDir))
                        {
                            imageEditForm.FileName = suggestedFileName;
                            imageEditForm.Comment = "Screen shot";
                            DialogResult res = imageEditForm.ShowDialog(this);

                            if (res == DialogResult.OK)
                            {
                                if (m_DataSource == null)
                                {
                                    MessageBox.Show("m_DataSource == null");
                                    return;
                                }

                                // Save off the image.
                                //
                                fileName = Path.Combine(m_TempDir, imageEditForm.FileName);

                                if (fileName.ToLower().EndsWith(".png") == false)
                                {
                                    fileName = String.Concat(fileName, ".png");
                                }
                                
                                imageEditForm.GetModifiedImage().Save(fileName, ImageFormat.Png);

                                comment = imageEditForm.Comment;
                            }
                        }
                    }

                    if (fileName != null)
                    {
                        Attachment a = new Attachment(fileName, comment);
                        m_DataSource.Attachments.Add(a);
                        m_AttachedFiles.Add(fileName);
                    
                        DialogResult res = MessageBox.Show(this,
                                                           "The screenshot was successfully attached.",
                                                           "Success",
                                                           MessageBoxButtons.OK,
                                                           MessageBoxIcon.Information);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}